package com.example.demo.topic.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.topic.entity.*;
import com.example.demo.topic.service.*;
import com.example.demo.topic.vo.TNoteManageVo;
import com.example.demo.util.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

/**
 * <p>
 * 发帖管理表; 前端控制器
 * </p>
 *
 * @author 涛
 * @since 2024-08-01
 */
@RestController
@RequestMapping("/tNoteManage")
@Tag(name = "tNoteManage", description = "发帖维护管理相关")
public class TNoteManageController {
    
    @Autowired
    private TNoteManageService tNoteManageService;

    @Autowired
    private TFansRecordService tFansRecordService;

    @Autowired
    private TNoteLikesService tNoteLikesService;

    @Autowired
    private TNoteCommentService tNoteCommentService;

    @Autowired
    private TViewRecordService tViewRecordService;

    /**
     * 获取帖子模块列表
     */
    @GetMapping("/getNoteList")
    @Operation(summary = "获取帖子列表接口", description = "获取帖子列表接口")
    public Result<?> getNoteList(@RequestParam(value = "current", required = false, defaultValue = "1")Integer current,
                                  @RequestParam(value = "size", required = false, defaultValue = "10")Integer size,
                                  TNoteManageVo note){
        Page<TNoteManageVo> page = new Page<>(current,size);
        return Result.ok(tNoteManageService.getNoteList(page,note));
    }

    /**
     * 根据ID查询帖子信息
     */
    @GetMapping("/getNoteById")
    @Operation(summary = "根据id查询帖子明细", description = "根据id查询帖子明细")
    public Result<?> getNoteById(Integer id){
        return Result.ok(tNoteManageService.getById(id));
    }

    /**
     * 修改帖子信息
     */
    @PutMapping("/updateNote")
    @Operation(summary = "根据id修改帖子明细", description = "根据id修改帖子明细")
    @Transactional
    public Result<?> updateNote(@RequestBody TNoteManage note){
        return Result.ok(tNoteManageService.updateById(note));
    }

    /**
     * 新增帖子信息
     */
    @PostMapping("/addNote")
    @Operation(summary = "新增帖子明细", description = "新增帖子明细")
    public Result<?> addNote(@RequestBody TNoteManage note){

        return Result.ok(tNoteManageService.save(note));
    }

    /**
     * 删除帖子信息
     */
    @DeleteMapping("/deleteNoteById")
    @Operation(summary = "删除帖子", description = "删除帖子")
    @Transactional
    public Result<?> deleteNoteById(Integer id){
        return Result.ok(tNoteManageService.removeById(id));
    }

    /**
     * 移动端查看帖子详情
     */
    @GetMapping("/viewNote")
    @Operation(summary = "移动端查看帖子详情", description = "移动端查看帖子详情")
    @Transactional
    public Result<?> viewNote(Integer id,Integer uid){
        return tNoteManageService.viewNote(id,uid);
    }

    /**
     * 移动端账号页面列表
     */
    @GetMapping("/getNoteByAccount")
    @Operation(summary = "移动端账号页面列表", description = "移动端账号页面列表")
    @Transactional
    public Result<?> getNoteByAccount(@RequestParam(value = "current", required = false, defaultValue = "1")Integer current,
                                      @RequestParam(value = "size", required = false, defaultValue = "10")Integer size,
                                      String code,Integer uid){
        Page<TNoteManageVo> page = new Page<>(current,size);
        return tNoteManageService.getNoteByAccount(page,code,uid);
    }

    /**
     * 获取主体账号下总点赞数
     */
    @GetMapping("/getAccountLikes")
    @Operation(summary = "获取主体账号下总点赞数", description = "获取主体账号下总点赞数")
    @Transactional
    public Result<?> getAccountLikes(String code){
        return tNoteManageService.getAccountLikes(code);
    }

    /**
     * 获取收藏的帖子
     */
    @GetMapping("/getCollectNote")
    @Operation(summary = "获取收藏的帖子", description = "获取收藏的帖子")
    @Transactional
    public Result<?> getCollectNote(@RequestParam(value = "current", required = false, defaultValue = "1")Integer current,
                                    @RequestParam(value = "size", required = false, defaultValue = "5")Integer size,
                                    Integer id){
        Page<TNoteManageVo> page = new Page<>(current,size);
        return tNoteManageService.getCollectNote(page,id);
    }

    /**
     * 服务端获取首页内容统计
     */
    @GetMapping("/getResultByUser")
    @Operation(summary = "服务端获取首页内容统计", description = "服务端获取首页内容统计")
    public Result<?> getResultByUser(Integer id){
        TNoteManageVo tNoteManageVo = new TNoteManageVo();
        //获取粉丝数
        QueryWrapper<TFansRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("count(*) as fansNum");
        queryWrapper.eq("MASTER_ID",id);
        tNoteManageVo.setFansNum(tFansRecordService.getOne(queryWrapper).getFansNum());

        LocalDateTime localDateTime = LocalDateTime.now();
        // 计算当前日期前七天的日期
        LocalDateTime sevenDaysAgo = localDateTime.minus(7, ChronoUnit.DAYS);

        // 获取近7日涨粉
        queryWrapper.select("count(*) as increaseNum");
        queryWrapper.ge("CREATED_TIME",sevenDaysAgo);
        tNoteManageVo.setIncreaseNum(tFansRecordService.getOne(queryWrapper).getIncreaseNum());

        //获取点赞数
        QueryWrapper<TNoteLikes> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.select("count(*) as likeAmount");
        queryWrapper2.eq("MASTER_ID",id);
        tNoteManageVo.setLikeAmount(tNoteLikesService.getOne(queryWrapper2).getLikeAmount());

        // 获取近7日获赞
        queryWrapper2.select("count(*) as increaseLikeNum");
        queryWrapper2.ge("CREATED_TIME",sevenDaysAgo);
        tNoteManageVo.setIncreaseLikeNum(tNoteLikesService.getOne(queryWrapper2).getIncreaseLikeNum());

        //获取评论数
        QueryWrapper<TNoteComment> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.select("count(*) as commentAmount");
        queryWrapper3.eq("MASTER_ID",id);
        tNoteManageVo.setCommentAmount(tNoteCommentService.getOne(queryWrapper3).getCommentAmount());

        // 获取近7日评论
        queryWrapper3.select("count(*) as increaseCommentNum");
        queryWrapper3.ge("CREATED_TIME",sevenDaysAgo);
        tNoteManageVo.setIncreaseCommentNum(tNoteCommentService.getOne(queryWrapper3).getIncreaseCommentNum());

        // 获取近7日浏览量
        QueryWrapper<TViewRecord> queryWrapper4 = new QueryWrapper<>();
        queryWrapper4.select("count(*) as viewNum");
        queryWrapper4.eq("MASTER_ID",id);
        queryWrapper4.ge("CREATED_TIME",sevenDaysAgo);
        tNoteManageVo.setViewNum(tViewRecordService.getOne(queryWrapper4).getViewNum());

        //获取发帖数量
        QueryWrapper<TNoteManage> queryWrapper5 = new QueryWrapper<>();
        queryWrapper5.select("count(*) as postNum");
        queryWrapper5.eq("MASTER_ID",id);
        tNoteManageVo.setPostNum(tNoteManageService.getOne(queryWrapper5).getPostNum());
        return Result.ok(tNoteManageVo);
    }
}

